/***************************************************************************
 *   Copyright (C) 2005 by xihe   *
 *   xihels@163.com                                                        *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#include "SendIM.h"
#include "QQClient.h"

SendIMPacket::SendIMPacket(QQClient& cl, 
	const	    string msg, 
	uint32_t    _receiver, 
	uint16_t    _messageId,
	uint8_t	    _fragmentSequence,
	uint8_t	    _replyType) :
    OutPacket(cl, QQ_CMD_SEND_IM, SEND_IM_COUNT),
    message(msg),
    messageId(_messageId),
    messageType(QQ_IM_NORMAL_TEXT),
    receiver(_receiver),
    totalFragments(0),
    fragmentSequence(_fragmentSequence),
    replyType(_replyType)
{}

int SendIMPacket::putBody(uint8_t* buf) const {
    uint8_t* cur = buf;

    cur += writeBufDw(cur, client.getUid());	// 发送者QQ号
    cur += writeBufDw(cur, receiver);		// 接收者QQ号
    cur += writeBufW(cur, source);		// 发送者QQ版本
    cur += writeBufDw(cur, client.getUid());	// 发送者QQ号
    cur += writeBufDw(cur, receiver);		// 接收者QQ号
    // 文件传输会话密钥
    cur += writeBufData(cur, client.getFileSessionKey(), QQ_KEY_LENGTH);
    // 消息类型
    cur += writeBufW(cur, messageType);
    // 顺序号
    cur += writeBufW(cur, sequence);
    // 发送时间
    cur += writeBufDw(cur, time(NULL));
    // 发送者头像
    cur += writeBufW(cur, client.getFace());
    // 字体信息，设成1
    cur += writeBufDw(cur, 1);
    // 分片数
    cur += writeBufB(cur, totalFragments);
    // 分片序号
    cur += writeBufB(cur, fragmentSequence);
    // 消息id
    cur += writeBufW(cur, messageId);

    /**
     * 初始化普通消息包的其余部分
     */
    // 消息方式，是发送的，还是自动回复的，1字节
    cur += writeBufB(cur, replyType);
    cur += writeBufData(cur, reinterpret_cast<const uint8_t*>(message.c_str()),
	    message.length());
    cur += font.putBody(cur);

    const uint8_t* pos = buf;
    
//    fprintf(stderr, "\n");
//    for (; pos < cur; pos++)
//	fprintf(stderr, "%02x ", *pos);
//    fprintf(stderr, "\n");
    return cur - buf;

}

